const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../config/api.js');

Page({
  data: {
    cartGoods: [],
    hiddenName: true,
    currentAmount: 0.00,
    cartTotal: {
      "goodsCount": 0,
      "goodsAmount": 0.00,
      "checkedGoodsCount": 0,
      "checkedGoodsAmount": 0.00
    },
    isEditCart: false,
    checkedAllStatus: false,
    editCartList: []
  },
	onShow: function () {
    app.globalData.checkedAddress = {}
		this.getCartList();
		this.setData({
			checkedAllStatus: false,
			currentAmount: 0
		});
	},
	getCartList: function () {
		let that = this;
		common.request(api.CartList, {}, 'get').then(function (data) {
			if (data.type === 'success') {
				that.setData({
					cartGoods: data.data.cartItemList
				});
			}
		});
	},
  getCurrentAmount: function () {
    let that = this;
    let cartItemIds = this.data.cartGoods.filter(function (element, index, array) {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });
    if (cartItemIds.length <= 0) {
      that.setData({
        currentAmount: 0.00
      });
      return false;
    }
    cartItemIds = cartItemIds.map(function (element, index, array) {
      if (element.checked == true) {
        return element.id;
      }
    });
		common.request(api.CartAmount, {cartItemIds: cartItemIds}, 'POST').then(function (data) {
      if (data.type == 'success') {
        that.setData({
          currentAmount: data.data.currentAmount
        });
      }
      that.setData({
        checkedAllStatus: that.isCheckedAll()
      });
    });
  },
	//判断购物车商品已全选
  isCheckedAll: function () {
    return this.data.cartGoods.every(function (element, index, array) {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });
  },
  checkedItem: function (event) {
    let itemIndex = event.target.dataset.itemIndex;
    let that = this;
    if (!this.data.isEditCart) {
      //非编辑状态
      let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
        if (index == itemIndex) {
          element.checked = !element.checked;
        }
        return element;
      });
      that.setData({
        cartGoods: tmpCartData,
        checkedAllStatus: that.isCheckedAll()
      });
    } else {
      //编辑状态
      let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
        if (index == itemIndex){
          element.checked = !element.checked;
        }
        return element;
      });
      that.setData({
        cartGoods: tmpCartData,
        checkedAllStatus: that.isCheckedAll()
      });
    }
    this.getCurrentAmount();
  },
  getCheckedGoodsCount: function(){
    let checkedGoodsCount = 0;
    this.data.cartGoods.forEach(function (v) {
      if (v.checked === true) {
        checkedGoodsCount = v.number;
       //checkedGoodsCount += v.number;//原版
      }
    });
    return checkedGoodsCount;
  },
  //全选
  checkedAll: function () {
    let that = this;
    var productIds = this.data.cartGoods.map(function (v) {
        return v.product_id;
      });
    if (!this.data.isEditCart) {
      console.log(111);
      //非编辑状态
      let checkedAllStatus = that.isCheckedAll();
      let tmpCartData = this.data.cartGoods.map(function (v) {
        v.checked = !checkedAllStatus;
        return v;
      });
      that.setData({
        cartGoods: tmpCartData,
        checkedAllStatus: that.isCheckedAll()
      });
    } else {
      //编辑状态
      let checkedAllStatus = that.isCheckedAll();
      let tmpCartData = this.data.cartGoods.map(function (v) {
        v.checked = !checkedAllStatus;
        return v;
      });
      that.setData({
        cartGoods: tmpCartData,
        checkedAllStatus: that.isCheckedAll()
      });
    }
    this.getCurrentAmount();
  },
  editCart: function () {
    var that = this;
    this.getCartList();
    if (this.data.isEditCart) {
      this.setData({
        isEditCart: !this.data.isEditCart
      });
    } else {
      //编辑状态
      let tmpCartList = this.data.cartGoods.map(function (v) {
        v.checked = false;
        return v;
      });
      this.setData({
        editCartList: this.data.cartGoods,
        cartGoods: tmpCartList,
        isEditCart: !this.data.isEditCart,
        checkedAllStatus: that.isCheckedAll(),
      });
    }
    this.getCurrentAmount();
  },
  updateCartCounts: function (cartItemId, quantity) {
    let that = this;
    let data = {
      cartItemId: cartItemId,
      quantity: quantity
    }
    common.request(api.CartUpdateCounts, data, 'POST').then(function (res) {
      that.setData({
        checkedAllStatus: that.isCheckedAll()
      });
    });
    this.getCurrentAmount();
  },
  //减少商品数量
  cutNumber: function (event) {
    let itemIndex = event.target.dataset.itemIndex;
    console.log(itemIndex);
    let cartItem = this.data.cartGoods[itemIndex];
    let quantity = (cartItem.quantity - 1 > 1) ? cartItem.quantity - 1 : 1;
    cartItem.quantity = quantity;
    this.setData({
      cartGoods: this.data.cartGoods
    });
    this.updateCartCounts(cartItem.id, quantity);
  },
  //增加商品数量
  addNumber: function (event) {
    let itemIndex = event.target.dataset.itemIndex;
    let cartItem = this.data.cartGoods[itemIndex];
    let quantity = cartItem.quantity;
    quantity++;
    cartItem.quantity = quantity;
    this.setData({
      cartGoods: this.data.cartGoods
    });
    this.updateCartCounts(cartItem.id, quantity);
  },
  deleteCart: function () {
    let that = this;
    let cartItemIds = this.data.cartGoods.filter(function (element, index, array) {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });
    if (cartItemIds.length <= 0) {
      return false;
    }
    cartItemIds = cartItemIds.map(function (element, index, array) {
      if (element.checked == true) {
        return element.id;
      }
    });
    common.request(api.CartDelete, {cartItemIds: cartItemIds}, 'POST').then(function (data) {
      if (data.type == 'success') {
        that.setData({
          cartGoods: data.data.cartItemList
        });
      }
      that.setData({
        checkedAllStatus: that.isCheckedAll()
      });
    });
    this.getCartList();
    let tmpCartList = this.data.cartGoods.map(function (v) {
      v.checked = false;
      return v;
    });
    this.setData({
      editCartList: this.data.cartGoods,
      cartGoods: tmpCartList,
      isEditCart: !this.data.isEditCart,
      checkedAllStatus: that.isCheckedAll(),
    });
    this.getCurrentAmount();
  },
	checkoutOrder: function () {
		let that = this;
		var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
			if (element.checked == true) {
				return true;
			} else {
				return false;
			}
		});
		if (checkedGoods.length <= 0) {
			common.showErrorToast('请添加并选择商品');
			return false;
		}

		app.globalData.checkedGoods = checkedGoods;
		app.globalData.goodsTotalPrice = that.data.currentAmount;

		wx.navigateTo({
			url: '../shopping/checkout/checkout'
		})
	}
})